package org.eclipse.soc.ide4edu.assignment.wizards;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.dialogs.ErrorDialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

public class AssignmentReferencesWizardPage extends WizardPage {

	private Text[] referenceTextField = new Text[50];

	private String references[] = new String[50];
	int count = 0;
	Composite composite;

	public AssignmentReferencesWizardPage(ISelection select) {
		super("TeacherWizardPage4");
		setTitle("New Assignment Wizard Page");
	}

	public void addReferences() {

		GridData gd = new GridData(GridData.FILL_HORIZONTAL);
		new Label(composite, SWT.NONE).setText("Reference " + (count + 1));
		referenceTextField[count] = new Text(composite, SWT.BORDER | SWT.MULTI);
		gd.widthHint = 20;
		gd.heightHint = 20;
		referenceTextField[count].setLayoutData(gd);
		count++;

		composite.layout();
		setControl(composite);

	}

	public void createControl(Composite parent) {

		composite = new Composite(parent, SWT.NONE);
		GridLayout gl = new GridLayout();
		composite.setLayout(gl);

		gl = new GridLayout();
		GridData gd = new GridData(GridData.FILL_HORIZONTAL);
		gl.numColumns = 2;

		Label referenceLabel = new Label(composite, SWT.NONE);
		referenceLabel.setText("Enter References ");
		gd.grabExcessHorizontalSpace = true;
		referenceLabel.setLayoutData(gd);
		Button more = new Button(composite, SWT.PUSH);
		more.setText("Click to Add");

		more.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				addReferences();
			}
		});

		addReferences();

	}

	public String[] getReferences() {
		String ref;

		for (int i = 0; i < count; i++) {
			if ((ref = referenceTextField[i].getText()) != null)
				references[i] = ref;
		}

		return references;
	}

	public void handleException(Exception e) {
		IStatus status = new Status(ERROR, "ReferencePageException", -1,
				"An Exception has Occured !", e);
		ErrorDialog error = new ErrorDialog(getShell(), "Error",
				e.getMessage(), status, 0);
		error.create();
		error.open();

	}
}